﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace A_ManageUsers
{
    public partial class BulkNewUserR : FloatableWindow
    {
        public BulkNewUserR()
        {
            InitializeComponent();
            var URoles = DataModels.UserRole.GetUserRoles("", new int[0]);
            foreach (var item in URoles)
            {
                item.NewObject();
            }
            RolesList.ItemsSource = URoles;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void CreateUsersToSave(object sender, RoutedEventArgs e)
        {
            var Users = new System.IO.StringReader(UsersBox.Text);
            while (Users.Peek() != -1)
            {
                var User = Users.ReadLine();//.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
                string Username = User.Substring(0, User.LastIndexOf("-"));
                string Password = User.Substring(User.LastIndexOf("-") + 1);
                DataModels.User U = new DataModels.User(new Services.Security.User());
                U.NewObject();
                U.UserName = Username;
                U.Password = Password;
                U.UserRoles = (ObservableCollection<DataModels.UserRole>)RolesList.ItemsSource;
            }
            SaveChanges.IsEnabled = true;
        }

        private void GeneratePasswords(object sender, RoutedEventArgs e)
        {
            var Users = new System.IO.StringReader(UsersBox.Text);
            var Passes = new System.IO.StringWriter();
            while (Users.Peek() != -1)
            {
                string User = Users.ReadLine();
                Passes.WriteLine(User + "-" + Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6));
            }
            UsersBox.Text = Passes.ToString();
            SaveG.Visibility = System.Windows.Visibility.Visible;
            GeneratePasswordsG.Visibility = System.Windows.Visibility.Collapsed;

        }

        bool isDragging = false;
        Point offset;
        Point InitalSize;

        //double WidthC = 0;
        //double HeightC = 0;
        //private void ReSize(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        //{
        //    WidthC += e.HorizontalChange/10;
        //    HeightC += e.VerticalChange/10;
        //}

        //private void StartResize(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
        //{

        //    WidthC = 0;
        //    HeightC = 0;
        //}

        //private void EndResize(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        //{
        //    this.Width += WidthC;
        //    this.Height += HeightC;
        //}

        private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isDragging = false;
            ((Button)sender).ReleaseMouseCapture();
        }

        private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isDragging = true;

            ((Button)sender).CaptureMouse();

            offset = e.GetPosition(null);
            InitalSize = new Point(this.Width, this.Height);
        }

        private void Button_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                Point NewPosition = e.GetPosition(null);

                this.Width = InitalSize.X - (NewPosition.X - offset.X);
                this.Height = InitalSize.Y - (NewPosition.Y - offset.Y);
            }
        }
    }
}

